
<template>
  <div class="search">
    <Row>
      <i-col>
        <Card>
          <Row>
            <Form ref="searchForm" :model="searchForm" inline :label-width="110" @keyup.enter.native="handleSearch()"
              class="search-form">
              <Row class="operation">
                <i-col span="6">
                  <Form-item label="姓名" prop="userRealName" style="margin-right: 150px">
                    <Input type="text" v-model="searchForm.userRealName" clearable placeholder="请输入姓名"
                      style="width: 200px" />
                  </Form-item>
                </i-col>
                <i-col span="6">
                  <Form-item label="身份证号码" prop="identityNumber" style="margin-right: 150px">
                    <Input type="text" v-model="searchForm.identityNumber" clearable placeholder="请输入身份证号码"
                      style="width: 200px" />
                  </Form-item>
                </i-col>

                <i-col span="6">
                  <Form-item label="合作商名称" prop="channelFullName" style="margin-right: 150px">
                    <Input type="text" v-model="searchForm.channelFullName" clearable placeholder="请输入合作商名称"
                      style="width: 200px" />
                  </Form-item>
                </i-col>

                <i-col span="6">
                  <Form-item label="业务类型" prop="businessType">
                    <Select v-model="searchForm.businessType" filterable clearable style="width: 200px">
                      <Option value="newCar">新车</Option>
                      <Option value="oldCar">二手车</Option>
                    </Select>
                  </Form-item>
                </i-col>
              </Row>

          

              <Row class="operation" style="text-align: center">
                <Form-item style="margin-left:-35px;" class="br">
                  <Button @click="handleSearch" type="primary" icon="ios-search">查询</Button>
                  <Button @click="handleReset" icon="md-redo">重置</Button>

                </Form-item>
              </Row>

            </Form>
          </Row>


          <Row>
            <Table :loading="loading" border :columns="columns" :data="data" sortable="custom"
              @on-sort-change="changeSort" @on-selection-change="showSelect" ref="table">


              <template slot-scope="{ row }" slot="businessType">
                <div v-if="row.businessType == 'oldCar'">
                  二手车
                </div>
                <div v-if="row.businessType == 'newCar'">
                  新车
                </div>
              </template>

              <template slot-scope="{ row }" slot="action">
               <Button style="margin-right:5px" type="primary" size="small" @click="edit(row)">审核</Button>
              </template>
            </Table>
          </Row>
          <Row type="flex" justify="end" class="page">
            <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize"
              @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small"
              show-total show-elevator show-sizer></Page>
          </Row>
        </Card>
      </i-col>
    </Row>


  </div>
</template>

<script>

import { getApplyUser,getUserById } from "@/projects/afs-channel/api/apply-user/user";

import hasRole from "_c/directives/hasRole";

export default {
  name: "apply-user",
  components: {},
  data() {
    const validateMobile = (rule, value, callback) => {
      if (value.length > 0) {
        var reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
        if (!reg.test(value)) {
          callback(new Error("手机号格式错误"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    return {
      loading: true,
      operationLoading: false,
      drop: false,
      dropDownContent: "更多",
      dropDownIcon: "ios-arrow-down",
      selectCount: 0,
      selectList: [],

      newCar: false, // 新车
      oldCar: false, // 旧车
      baseInfo: [], //合作商集合
      statusList: [], // 状态的数据字典

      searchForm: {
        businessType: null, //权限类型
        pageNumber: 1,
        pageSize: 10,
        userRealName: "",
        identityNumber: "",
        businessType: "",
        channelFullName: "",

      },

      hisSearchForm: {
        businessType: null, //权限类型
        pageNumber: 1,
        pageSize: 10,
        userRealName: "",
        identityNumber: "",
        businessType: "",
        channelFullName: "",

      },

      modalType: 0,
      userModalVisible: false,
      modalTitle: "请输入停用原因！",
      submitLoading: false,
      //删除提示框
      modal2: false,
      modal_loading: false,
      // 下面是存提示窗的人
      sysUser: [], //有权限账号的见证人
      carWitness: [], // 正在见证进件的人

      
      // 下面是用于权限控制的 01:代表新车  02 代表二手车  03代表新车二手车都有
      businessType: null,
      ownBusinessType: null,

      // 下面是代表管理员的权限 01:代表新车  02 代表二手车  03代表新车二手车都有
      businessManage: null,
      ownBusinessManage: null,


      // 应有的字段信息
      columns: [
        {
          title: "序号",
          type: "index",
          minWidth: 70,
          align: "center",
          fixed: "left"
        },
        {
          title: "合作商名称",
          key: "channelFullName",
          align: "center",
          fixed: "left",
          minWidth: 145,
          ellipsis: true, // 开启后，文本将不换行，超出部分显示为省略号
          tooltip: true // 鼠标放上去提示
        },
           {
          title: "用户名",
          key: "userRealName",
          minWidth: 100,
          align: "center",
          ellipsis: true,
          tooltip: true // 鼠标放上去提示
        },
        {
          title: "业务类型",
          key: "businessType",
          minWidth: 120,
          maxWidth: 120,
          align: "center",
          slot: "businessType"
        },
        {
          title: "身份证号码",
          key: "identityNumber",
          minWidth: 160,
          align: "center"
        },
        {
          title: "申请时间",
          key: "createTime",
          align: "center",
          minWidth: 200,
          ellipsis: true, // 开启后，文本将不换行，超出部分显示为省略号
          tooltip: true // 鼠标放上去提示
        },
        {
          title: "操作",
          key: "action",
          minWidth: 150,
          align: "center",
          fixed: "right",
          slot: "action"
        }
      ],
      data: [],
      total: 0,
      // 停用的表单
      userForm: {
        startStopReason: null //停用原因
      },
      userFormValidate: {
        startStopReason: [
          { required: true, message: "请输入原因", trigger: "blur" }
        ]
      },
      limitPut: [
        { required: false, message: "请输入正确的手机号", trigger: "blur" },
        { validator: validateMobile, trigger: "blur" }
      ]
    };
  },
  watch: {
    $route(to, from) {
      if (
        from.path.indexOf("apply-user-examine") > -1 ||
        from.path.indexOf("witness-edit") > -1 ||
        from.path.indexOf("witness-examine") > -1
      ) {
        this.handleReset();
      }
    }
  },
  methods: {
    init() {
      this.getUser();
      // 获取权限顺便查询合作商信息
      this.getRole();
      this.getManage();
    },
    getManage(){
      console.log("我要过来判断权限");
      console.log(this.$store.state.user.roles.toString());
      // 判断是管理员  全都可以操作
      if (this.$store.state.user.roles.toString().indexOf("ROLE_ADMIN") != -1) {
        this.businessManage = "03";
        this.ownBusinessManage = "03";
        console.log("我是管理员");
      } else {
        // 说明不是管理员 那么需要判断他有哪些管理角色
        if (
          this.$store.state.user.roles
            .toString()
            .indexOf("ROLE_OTHER_NEW_MANAGE_WITNESS") != -1
        ) {
          // 说明是新车非直营管理
          this.businessManage = "01";
          console.log("我是非直营新车");
        }
        if (
          this.$store.state.user.roles
            .toString()
            .indexOf("ROLE_OTHER_OLD_MANAGE_WITNESS") != -1
        ) {
          // 说明是二手车非直营管理
          if (this.businessManage == "01") {
            this.businessManage = "03";
            console.log("我是非直营新车二手车");
          } else {
            this.businessManage = "02";
            console.log("我是非直营二手车");
          }
        }
        // 再去判断直营的角色属性
        if (
          this.$store.state.user.roles
            .toString()
            .indexOf("ROLE_OWN_NEW_MANAGE_WITNESS") != -1
        ) {
          //我是直营的新车属性
          this.ownBusinessManage = "01";
          console.log("我是直营新车");
        }
        if (
          this.$store.state.user.roles
            .toString()
            .indexOf("ROLE_OWN_OLD_MANAGE_WITNESS") != -1
        ) {
          if (this.ownBusinessManage == "01") {
            this.ownBusinessManage = "03";
            console.log("我是直营新车二手车");
          } else {
            this.ownBusinessManage = "02";
            console.log("我是二手车直营");
          }
        }
      }
    },
    // 查询自己的权限
    getRole() {
      console.log("我要过来判断权限");
      console.log(this.$store.state.user.roles.toString());
      // 判断是管理员  全都可以操作
      if (this.$store.state.user.roles.toString().indexOf("ROLE_ADMIN") != -1) {
        this.businessType = "03";
        this.ownBusinessType = "03";
        console.log("我是管理员");
      } else {
        // 说明不是管理员 那么需要判断他有哪些管理角色
        if (
          this.$store.state.user.roles
            .toString()
            .indexOf("ROLE_OTHER_NEW_MANAGE_WITNESS") != -1 ||this.$store.state.user.roles
            .toString()
            .indexOf("ROLE_OTHER_NEW_SEE_WITNESS") != -1
        ) {
          // 说明是新车非直营管理
          this.businessType = "01";
          console.log("我是非直营新车");
        }
        if (
          this.$store.state.user.roles
            .toString()
            .indexOf("ROLE_OTHER_OLD_MANAGE_WITNESS") != -1 ||this.$store.state.user.roles
            .toString()
            .indexOf("ROLE_OTHER_OLD_SEE_WITNESS") != -1
        ) {
          // 说明是二手车非直营管理
          if (this.businessType == "01") {
            this.businessType = "03";
            console.log("我是非直营新车二手车");
          } else {
            this.businessType = "02";
            console.log("我是非直营二手车");
          }
        }
        // 再去判断直营的角色属性
        if (
          this.$store.state.user.roles
            .toString()
            .indexOf("ROLE_OWN_NEW_MANAGE_WITNESS") != -1 ||this.$store.state.user.roles
            .toString()
            .indexOf("ROLE_OWN_NEW_SEE_WITNESS") != -1
        ) {
          //我是直营的新车属性
          this.ownBusinessType = "01";
          console.log("我是直营新车");
        }
        if (
          this.$store.state.user.roles
            .toString()
            .indexOf("ROLE_OWN_OLD_MANAGE_WITNESS") != -1|| this.$store.state.user.roles
            .toString()
            .indexOf("ROLE_OWN_OLD_SEE_WITNESS") != -1
        ) {
          if (this.ownBusinessType == "01") {
            this.ownBusinessType = "03";
            console.log("我是直营新车二手车");
          } else {
            this.ownBusinessType = "02";
            console.log("我是二手车直营");
          }
        }
      }
    },

    // 改变一页多少个
    changePage(v) {
      console.log("点击下面的页码" + v);
      this.searchForm.pageNumber = v;
      this.hisSearchForm.pageNumber = v;
      // this.getUser();
      this.getHisUser();
      this.clearSelectAll();
    },
    // 一页多少个
    changePageSize(v) {
      console.log("这个是啥");
      this.searchForm.pageSize = v;
      this.hisSearchForm.pageSize = v;
      // this.getUser();
      this.getHisUser();
      this.clearSelectAll();
    },
    // 查找见证人
    getUser() {

      // 多条件查询见证人列表
      this.loading = true;
      this.loading = true;
      if (
        this.searchForm.witnessName != null &&
        this.searchForm.witnessName != undefined &&
        this.searchForm.witnessName != ""
      ) {
        this.searchForm.witnessName = this.searchForm.witnessName.replace(
          /\s*/g,
          ""
        );
      }
      if (
        this.searchForm.witnessIdCard != null &&
        this.searchForm.witnessIdCard != undefined &&
        this.searchForm.witnessIdCard != ""
      ) {
        this.searchForm.witnessIdCard = this.searchForm.witnessIdCard.replace(
          /\s*/g,
          ""
        );
      }
      if (
        this.searchForm.witnessContactNumber != null &&
        this.searchForm.witnessContactNumber != undefined &&
        this.searchForm.witnessContactNumber != ""
      ) {
        this.searchForm.witnessContactNumber = this.searchForm.witnessContactNumber.replace(
          /\s*/g,
          ""
        );
      }
      getApplyUser(this.searchForm).then(res => {
        this.loading = false;
        if (res.code === "0000") {
          this.data = res.data.records;
          this.total = res.data.total;
        }
      });
    },
    // 调用历史的搜索数据
    getHisUser() {

      // 多条件查询见证人列表
      this.loading = true;
      if (
        this.hisSearchForm.witnessName != null &&
        this.hisSearchForm.witnessName != undefined &&
        this.hisSearchForm.witnessName != ""
      ) {
        this.hisSearchForm.witnessName = this.hisSearchForm.witnessName.replace(
          /\s*/g,
          ""
        );
      }
      if (
        this.hisSearchForm.witnessIdCard != null &&
        this.hisSearchForm.witnessIdCard != undefined &&
        this.hisSearchForm.witnessIdCard != ""
      ) {
        this.hisSearchForm.witnessIdCard = this.hisSearchForm.witnessIdCard.replace(
          /\s*/g,
          ""
        );
      }
      if (
        this.hisSearchForm.witnessContactNumber != null &&
        this.hisSearchForm.witnessContactNumber != undefined &&
        this.hisSearchForm.witnessContactNumber != ""
      ) {
        this.hisSearchForm.witnessContactNumber = this.hisSearchForm.witnessContactNumber.replace(
          /\s*/g,
          ""
        );
      }
      getApplyUser(this.hisSearchForm).then(res => {
        this.loading = false;
        if (res.code === "0000") {
          this.data = res.data.records;
          this.total = res.data.total;
        }
      });
    },

    //查询按钮操作
    handleSearch() {
      // if(this.searchForm.witnessContactNumber.length>0){
      //   var reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
      //   if (!reg.test(this.searchForm.witnessContactNumber)) {
      //    this.$Message.warning("请输入正确的手机号！");
      //   return;
      //   }

      // }
      // 0 ：代表新车二手车都查 1：代表只查二手车 2：代表只查新车
      if (this.newCar && this.oldCar) {
        this.searchForm.businessAttributes = "00";
      } else if (this.oldCar) {
        this.searchForm.businessAttributes = "1";
      } else if (this.newCar) {
        this.searchForm.businessAttributes = "0";
      } else {
        this.searchForm.businessAttributes = "00";
      }
      this.searchForm.pageNumber = 1;
      // 把搜索的参数赋值给历史记录

      this.hisSearchForm.pageNumber = this.searchForm.pageNumber;
      this.hisSearchForm.pageSize = this.searchForm.pageSize;
      this.hisSearchForm.witnessName = this.searchForm.witnessName;
      this.hisSearchForm.witnessIdCard = this.searchForm.witnessIdCard;
      this.hisSearchForm.businessAttributes = this.searchForm.businessAttributes;
      this.hisSearchForm.witnessContactNumber = this.searchForm.witnessContactNumber;
      this.hisSearchForm.status = this.searchForm.status;
      this.hisSearchForm.channelId = this.searchForm.channelId;
      this.hisSearchForm.businessType = this.searchForm.businessType;
      this.getUser();
    },
    // 点击重置
    handleReset() {
      this.$refs.searchForm.resetFields();
      this.searchForm.witnessName = "";
      this.searchForm.witnessIdCard = "";
      this.searchForm.businessAttributes = "";
      this.searchForm.witnessContactNumber = "";
      this.searchForm.status = "";
      this.searchForm.channelId = null;
      this.searchForm.pageNumber = 1;
      this.newCar = false;
      this.oldCar = false;

      // 把搜索的参数赋值给历史记录
      this.hisSearchForm.pageNumber = this.searchForm.pageNumber;
      this.hisSearchForm.pageSize = this.searchForm.pageSize;
      this.hisSearchForm.witnessName = this.searchForm.witnessName;
      this.hisSearchForm.witnessIdCard = this.searchForm.witnessIdCard;
      this.hisSearchForm.businessAttributes = this.searchForm.businessAttributes;
      this.hisSearchForm.witnessContactNumber = this.searchForm.witnessContactNumber;
      this.hisSearchForm.status = this.searchForm.status;
      this.hisSearchForm.channelId = this.searchForm.channelId;
      this.hisSearchForm.businessType = this.searchForm.businessType;
      this.getUser();
    },
    // 排序
    changeSort(e) {
      this.searchForm.sort = e.key;
      this.searchForm.order = e.order;
      if (e.order === "normal") {
        this.searchForm.order = "";
      }
      this.getHisUser();
    },
    // 跳转编辑
    edit(v) { 
      getUserById(v)
        .then(res => {
          if (res.code === "0000") {
            this.afs.newTab(
              this,
              "projects/afs-channel/pages/apply-user/applyUserExamine",
              "审核:" + v.username,
              "ios-add",
              { id: v.id },
              "apply-user-examine" + v.id,
              [],
              true
            );
          }
        })
        .catch(() => {});


      
    },


    showSelect(e) {
      this.selectList = e;
      this.selectCount = e.length;
    },
    clearSelectAll() {
      this.$refs.table.selectAll(false);
    },

  
  },
  // 遍及用户v

  mounted() {
    this.init();
  }
};
</script>
